/**
 * 
 */
package edu.cmu.cs.webapp.ioms.dao.impl;

import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import edu.cmu.cs.webapp.ioms.dao.DataAccessException;
import edu.cmu.cs.webapp.ioms.dao.UserDAO;
import edu.cmu.cs.webapp.ioms.models.Client;
import edu.cmu.cs.webapp.ioms.models.IOMSPackage;
import edu.cmu.cs.webapp.ioms.models.OrderStatus;
import edu.cmu.cs.webapp.ioms.models.PurchaseOrder;
import edu.cmu.cs.webapp.ioms.models.User;

/**
 * @author Christian
 * 
 */
public class UserDAOImpl extends DAO implements UserDAO {

	@Override
	public List<User> getAllUsers() throws DataAccessException {
		// TODO Auto-generated method stub
		Connection con = null;
		User user = null;
        List<User> usrlst = new ArrayList<User>();
        try {
        	con = getConnection();
        	Statement stmt = con.createStatement();
        	String sql = "select * from user";
        	ResultSet rs = stmt.executeQuery(sql);
        	while(rs.next()){
        		user = IOMSPackage.eINSTANCE.getIOMSFactory().createUser();
        		
        		user.setUser_id(rs.getInt("user_id"));
        		user.setName(rs.getString("name"));
        		user.setLogin(rs.getString("login"));
        		user.setPassword(rs.getString("password"));
        		user.setRole(rs.getString("role"));
        		user.setDepartment(rs.getString("department"));

        		usrlst.add(user);
        	}
            stmt.close();
            con.close();    	
        } catch (Exception e) {
            try { if (con != null) con.close(); } catch (SQLException e2) { /* ignore */ }
            throw new DataAccessException(e);
        }
        return usrlst;
	}

	@Override
	public boolean addUser(User user) throws DataAccessException {
		// TODO Auto-generated method stub
		Connection con = null;
		try {
			con = getConnection();
			
            PreparedStatement pstmt = con.prepareStatement(
            "insert into user(name, login, password, role, department)" +
            "values (? ,?, ?, ?, ?)");
            
            pstmt.setString(1, user.getName());
            pstmt.setString(2, user.getLogin());
            pstmt.setString(3, user.getPassword());
            pstmt.setString(4, user.getRole());
            pstmt.setString(5, user.getDepartment());
            
            pstmt.executeUpdate();
            System.out.println("insert: " + user.toString());
            		
			pstmt.close();
			con.close();
			
			log("Create user [" + user.toString() + "]", "Add");

		} catch (Exception e) {
            try { if (con != null) con.close(); } catch (SQLException e2) { /* ignore */ }
            throw new DataAccessException(e);
		}
		return true;
	}

	@Override
	public boolean deleteUser(User user) throws DataAccessException {
		// TODO Auto-generated method stub
		Connection con = null;
		try {
			con = getConnection();		
            PreparedStatement pstmt = con.prepareStatement(
            "delete from user where user_id =" + user.getUser_id());
            pstmt.executeUpdate();
            System.out.println("delete: " + user.toString());
            		
			pstmt.close();
			con.close();
			
			log("Delete user [" + user.toString() + "]", "Delete");

		} catch (Exception e) {
            try { if (con != null) con.close(); } catch (SQLException e2) { /* ignore */ }
        	throw new DataAccessException(e);
		}
		return true;
	}

	@Override
	public User findUser(String login) throws DataAccessException {
		// TODO Auto-generated method stub
		Connection con = null;
		User user = null;

        try {
        	con = getConnection();
        	Statement stmt = con.createStatement();
        	String sql = "select * from user where login='" + login +"'" ;
        	ResultSet rs = stmt.executeQuery(sql);
        	if(rs.next()){
        		user = IOMSPackage.eINSTANCE.getIOMSFactory().createUser();
        		
        		user.setUser_id(rs.getInt("user_id"));
        		user.setName(rs.getString("name"));
        		user.setLogin(rs.getString("login"));
        		user.setPassword(rs.getString("password"));
        		user.setRole(rs.getString("role"));
        		user.setDepartment(rs.getString("department"));       		
        	}
            stmt.close();
            con.close();    	
        } catch (Exception e) {
            try { if (con != null) con.close(); } catch (SQLException e2) { /* ignore */ }
            throw new DataAccessException(e);
        }
        return user;
	}

	@Override
	public boolean updateUser(User user) throws DataAccessException {
		// TODO Auto-generated method stub
		Connection con = null;
		try {
			con = getConnection();
			
			String str="update user set ";
			if( user.getName() != null)
				str += "name='" + user.getName() +"',";
			if( user.getLogin() != null )
				str += "login='" + user.getLogin() + "',";
			if( user.getPassword() != null)
				str += "password='" + user.getPassword() +"',";
			if( user.getRole() != null )
				str += "role='" + user.getRole() +"',";
			if( user.getDepartment() != null )
				str += "department='" + user.getDepartment() +"',";	

			str = str.substring(0, str.lastIndexOf(","));
			str += " where user_id=" + user.getUser_id();	
			System.out.println(str);
            PreparedStatement pstmt = con.prepareStatement(str);
            pstmt.executeUpdate();
            System.out.println("update: " + user.toString());
            		
			pstmt.close();
			con.close();
			
			log("Update user [" + user.toString() + "]", "Update");

		} catch (Exception e) {
            try { if (con != null) con.close(); } catch (SQLException e2) { /* ignore */ }
            throw new DataAccessException(e);
		}
		return true;
	}

}
